package com.yww.leetcode.stack;

import com.yww.datastructure.stack.LinkedListStack;
import com.yww.datastructure.stack.Stack;

/**
 * @author yww
 * @description 150. 逆波兰表达式求值
 * @since 2024/2/5 10:08
 */
public class EvalRPN150 {
    // 给你一个字符串数组 tokens ，表示一个根据 逆波兰表示法 表示的算术表达式。
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new LinkedListStack<>();
        for (int i = 0; i < tokens.length; i++) {
            String token = tokens[i];
            switch (token) {
                case "+": {
                    Integer a = stack.pop();
                    Integer b = stack.pop();
                    stack.push(b + a);
                    break;
                }
                case "-": {
                    Integer a = stack.pop();
                    Integer b = stack.pop();
                    stack.push(b - a);
                    break;
                }
                case "*": {
                    Integer a = stack.pop();
                    Integer b = stack.pop();
                    stack.push(b * a);
                    break;
                }
                case "/": {
                    Integer a = stack.pop();
                    Integer b = stack.pop();
                    stack.push(b / a);
                    break;
                }
                default: {
                    stack.push(Integer.valueOf(token));
                }
            }
        }
        return stack.pop();
    }
}
